
# Import other required libs
import tarfile
import platform

# What we should build
sources = ['src/cleartext_stream.c', 'src/listener.c', 'src/ssl_stream.c', 'src/stream.c', 'src/nettles.c']
target = 'nettles'
deps = 'deps'
platform_extra_libs = []

distclean = 'distclean'

if (platform.system() == 'Windows'):
# On Windows we must
# use mingw 
# have the path to sh, sed, awk et al
# link with ws2_32
  tools = ['mingw']
  platform_extra_libs = ['ws2_32']
  env = Environment(tools = tools)
  path_to_msys = ARGUMENTS.get('path_to_msys') 
  if (not path_to_msys):
    print 'No path_to_msys! Build will most likely fail.'
  env.PrependENVPath('PATH', path_to_msys)
elif (platform.system() == 'Linux'):
  platform_extra_libs = ['m', 'rt']
  tools = ['default']
  env = Environment(tools = tools)
elif (platform.system() == 'Darwin'):
  tools = ['default']
  env = Environment(tools = tools)
else:
  print 'Platform not supported'
  Exit(2)

# Check for platform differences
conf = env.Configure(config_h='src/config.h')
conf.CheckFunc('_stricmp')
conf.CheckFunc('stricmp')
conf.CheckFunc('strcasecmp')
for lib in platform_extra_libs:
  if not conf.CheckLib(lib):
    print 'Failed to find library:' + lib
    print 'Please install it and restart scons'
    Exit(7)
conf.Finish()

# Check for debug build
debug = ARGUMENTS.get('debug', 0)
if int(debug):
  env.Append(CCFLAGS = '-g')

# Simple function to untar dependencies
def untar(target, source, env):
    tar = tarfile.open(str(source[0]))
    # Hardcoded path instead of target[0]
    tar.extractall(str(deps))
    tar.close()

# libevent
libevent_tgz = deps + '/libevent-2.0.21-stable.tar.gz'
libevent_path = deps + '/libevent-2.0.21-stable'
libevent_include_path = libevent_path + '/include'
libevent_lib_path = libevent_path + '/.libs'
libevent_lib = ['event_core', 'event_extra']

if not env.GetOption('clean'):
  env.Command(Dir(libevent_path),libevent_tgz,untar)
  libevent_env = Environment(LIBPATH=libevent_lib_path, tools = tools);
  libevent_conf = Configure(libevent_env)
  if not libevent_conf.CheckLib(libevent_lib):
    print 'Could not find: ', libevent_lib
    if (platform.system() == 'Windows'):
      env.Command(libevent_lib, Dir(libevent_lib_path), ['sh ./configure --disable-shared --disable-openssl','make'], chdir=libevent_path)
    else:
      env.Command(libevent_lib, Dir(libevent_lib_path), ['./configure --disable-shared --disable-openssl','make'], chdir=libevent_path)
    env.Depends(target, libevent_lib)
  libevent_env = libevent_conf.Finish();
else:
  env.Clean(distclean, Dir(libevent_path))

# polarssl
polarssl_tgz = deps + '/polarssl-1.2.8-gpl.tgz' 
polarssl_path = deps + '/polarssl-1.2.8'
polarssl_include_path = polarssl_path + '/include'
polarssl_lib_path = polarssl_path + '/library'
polarssl_lib = ['polarssl']

if not env.GetOption('clean'):
  env.Command(Dir(polarssl_path),polarssl_tgz,untar)
  polarssl_env = Environment(LIBPATH=polarssl_lib_path, tools = tools);
  polarssl_conf = Configure(polarssl_env)
  if not polarssl_conf.CheckLib(polarssl_lib):
    print 'Could not find: ', polarssl_lib
    env.Command(polarssl_lib, Dir(polarssl_lib_path), ['make lib'], chdir=polarssl_path)
    env.Depends(target, polarssl_lib)
  polarssl_env = polarssl_conf.Finish()
else:
  env.Clean(distclean, Dir(polarssl_path))

# lua
lua_tgz = deps + '/lua-5.2.2.tar.gz'
lua_path = deps + '/lua-5.2.2'
lua_include_path = lua_path + '/src'
lua_lib_path = lua_path + '/src'
lua_lib = ['lua']

if not env.GetOption('clean'):
  env.Command(Dir(lua_path),lua_tgz,untar)
  lua_env = Environment(LIBPATH=lua_lib_path, tools = tools);
  lua_conf = Configure(lua_env)
  if not lua_conf.CheckLib(lua_lib):
    print 'Could not find: ', lua_lib
    if (platform.system() == 'Windows'):
      env.Command(lua_lib, Dir(lua_lib_path), ['make mingw'], chdir=lua_path)
    elif (platform.system() == 'Linux'):
      env.Command(lua_lib, Dir(lua_lib_path), ['make linux'], chdir=lua_path)
    else:
      env.Command(lua_lib, Dir(lua_lib_path), ['make macosx'], chdir=lua_path)
    env.Depends(target, lua_lib)
  lua_env = lua_conf.Finish()
else:
  env.Clean(distclean, Dir(lua_path))

# Add includes and libs
env.Append(CPPPATH=[libevent_include_path, lua_include_path, polarssl_include_path])
env.Append(LIBPATH=[libevent_lib_path, lua_lib_path, polarssl_lib_path])
env.Append(LIBS=[libevent_lib, lua_lib, polarssl_lib, platform_extra_libs])

env.Program(target, sources)

